package com.gears_software.sxdc.dccx.data.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.gears_software.sxdc.dccx.ui.map.LocationTask;
import com.gears_software.sxdc.dccx.ui.map.RegeocodeTask;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import com.orhanobut.logger.Logger;

import org.androidannotations.annotations.EService;
import org.androidannotations.annotations.UiThread;
import org.json.JSONObject;

import java.net.URISyntaxException;
import java.util.Date;

/**
 * Created by chezhenzhen on 2017/10/27.
 */

public class TestService extends Service {

    private Socket mSocket;


    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            mSocket.emit("send", "a");
            if (!isConnect){
                mSocket.emit("send", "a");
            }
            //要做的事情，这里再次调用此Runnable对象，以实现每两秒实现一次的定时器操作
            handler.postDelayed(this, 30000);
        }
    };
    boolean isConnect;

    @Override
    public void onCreate() {
        super.onCreate();
        initSocketHttp();
        connectSocket();
        handler.postDelayed(runnable, 30000);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    /**
     * 初始化Socket,Http的连接方式
     */
    private void initSocketHttp() {
        try {
            mSocket = IO.socket("http://192.168.0.140:8083"); // 初始化Socket
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private void connectSocket() {
        mSocket.connect();
        mSocket.on(Socket.EVENT_CONNECT, onConnect);// 连接成功
        mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);// 断开连接

        mSocket.on( Socket.EVENT_CONNECT_ERROR, onConnectError );// 连接异常
        mSocket.on( Socket.EVENT_CONNECT_TIMEOUT, onConnectTimeoutError );// 连接超时
        mSocket.on("sendclient", onConnectMsg);// 监听消息事件回调
    }

    private void disConnectSocket() {
        mSocket.disconnect();
        mSocket.off(Socket.EVENT_CONNECT, onConnect);// 连接成功
        mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);// 断开连接
        mSocket.off( Socket.EVENT_CONNECT_ERROR, onConnectError );// 连接异常
        mSocket.off( Socket.EVENT_CONNECT_TIMEOUT, onConnectTimeoutError );// 连接超时
        mSocket.off("sendclient", onConnectMsg);// 监听消息事件回调
    }

    private Emitter.Listener onConnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {

            isConnect = true;
            Logger.d(new Date() + " " + "连接成功" + isConnect);
        }
    };

    private Emitter.Listener onDisconnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            isConnect = false;
            Logger.d(new Date() + " " + "连接断开" + isConnect);
        }
    };

    private Emitter.Listener onConnectError = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            isConnect = false;
            Logger.d(new Date() + " " + "连接错误" + args[0]);
        }
    };

    private Emitter.Listener onConnectTimeoutError = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            isConnect = false;
            Logger.d(new Date() + " " + "连接超时" + args[0]);
        }
    };


    private Emitter.Listener onConnectMsg = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            Logger.d(new Date() + " " + "消息回调" + args[0]);
        }
    };

//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        disConnectSocket();
//        handler.removeCallbacks(runnable);
//    }
}
